package com.abe.bathsystem.widget.dialog;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;

import net.yet.campusdev.R;
import com.abe.bathsystem.adapter.AbeCommonAdapter;
import com.abe.bathsystem.adapter.AbeMultiItemTypeAdapter;
import com.abe.bathsystem.adapter.AbeViewHolder;
import com.abe.bathsystem.widget.OnOperateListener;
import com.abe.bathsystem.widget.recyclerview.DefaultItemDecoration;
import com.abe.libcore.utils.normal.CommonUtils;
import com.abe.libcore.utils.normal.ToastUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ChooseMenuDialog extends BaseDialog {
    private String[] autoLock = {"5秒", "10秒", "20秒", "30秒", "40秒", "50秒", "1分钟", "1分钟30秒", "2分钟", "2分钟30秒", "3分钟", "3分钟30秒", "4分钟", "4分钟30秒", "5分钟"};
    private String[] adUpdate = {"1小时", "2小时", "3小时", "4小时", "5小时", "6小时", "7小时", "8小时", "9小时", "10小时",
            "11小时", "12小时", "13小时", "14小时", "15小时", "16小时", "17小时", "18小时", "19小时", "20小时", "21小时", "22小时", "23小时", "1天"};

    public interface OnChooseMenuListener {
        void onChoose(int type, String data);
    }

    public static final int TYPE_AUTO_LOCK = 0x001;
    public static final int TYPE_AD_UPDATE = 0x002;

    private OnChooseMenuListener listener;
    private List<String> menuData;
    private int type;

    public ChooseMenuDialog(@NonNull Context context, OnOperateListener operateListener, int type) {
        super(context, operateListener);
        this.type = type;
    }

    public ChooseMenuDialog(@NonNull Context context, int type) {
        super(context);
        this.type = type;
    }

    public void setListener(OnChooseMenuListener listener) {
        this.listener = listener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.item_view_v3_setting_menu);
        switch (type) {
            case TYPE_AUTO_LOCK: {
                menuData = Arrays.asList(autoLock);
            }
            break;
            case TYPE_AD_UPDATE: {
                menuData = Arrays.asList(adUpdate);
            }
            break;
        }
        initRV();
    }

    private void initRV() {
        RecyclerView itemRv = findViewById(R.id.item_rv);
        if (menuData == null) menuData = new ArrayList<>();
        AbeCommonAdapter<String> adapter = new AbeCommonAdapter<String>(getContext(), R.layout.item_recycler_v3_set_menu, menuData) {
            @Override
            protected void convert(AbeViewHolder holder, String entity, int position) {
                holder.setText(R.id.item_txt_menu, entity);
            }
        };
        adapter.setOnItemClickListener(new AbeMultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (listener != null) {
                    listener.onChoose(type, menuData.get(position));
                } else {
                    ToastUtils.show("未设置回调监听");
                }
                dismiss();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        itemRv.setLayoutManager(new LinearLayoutManager(getContext()));
        itemRv.addItemDecoration(new DefaultItemDecoration(CommonUtils.getColor(R.color.cbd)));
        itemRv.setAdapter(adapter);
    }
}